package com.shizhefei.indicator.moretab;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.shizhefei.fragment.LazyFragment;
import com.shizhefei.indicator.demo.R;
import com.shizhefei.view.indicator.BannerComponent;
import com.shizhefei.view.indicator.FixedIndicatorView;
import com.shizhefei.view.indicator.IndicatorViewPager;
import com.shizhefei.view.viewpager.RecyclingPagerAdapter;

/**
 * viewpager嵌套
 * 1. 嵌套 轮播banner不冲突
 */
public class MoreFragmentWithBanner extends LazyFragment {
    private ProgressBar progressBar;
    private TextView textView;
    private int tabIndex;
    public static final String INTENT_INT_INDEX = "intent_int_index";

    //banner
    private BannerComponent bannerComponent;
    private LayoutInflater inflate;
    private FixedIndicatorView indicator;

    @Override
    protected void onCreateViewLazy(Bundle savedInstanceState) {
        super.onCreateViewLazy(savedInstanceState);
        handler = new Handler(Looper.getMainLooper()) {
            public void handleMessage(android.os.Message msg) {
                progressBar.setVisibility(View.GONE);
                textView.setVisibility(View.VISIBLE);
            }
        };

        setContentView(R.layout.fragment_tabmain_item_with_banner);
        tabIndex = getArguments().getInt(INTENT_INT_INDEX);
        progressBar = (ProgressBar) findViewById(R.id.fragment_mainTab_item_progressBar);
        textView = (TextView) findViewById(R.id.fragment_mainTab_item_textView);
        textView.setText("界面" + " " + tabIndex + " 加载完毕");
        handler.sendEmptyMessageDelayed(1, 500);
        initBanner();
    }

    private void initBanner() {
        inflate = LayoutInflater.from(getApplicationContext());

        //ViewPager,Indicator
        ViewPager viewPager = (ViewPager) findViewById(R.id.banner_viewPager);
        indicator = (FixedIndicatorView) findViewById(R.id.banner_indicator);

        bannerComponent = new BannerComponent(indicator, viewPager, false);
        bannerComponent.setAdapter(adapter);
        bannerComponent.setAutoPlayTime(2500);
    }

    @Override
    protected void onResumeLazy() {
        super.onResumeLazy();
        bannerComponent.startAutoPlay();
    }

    @Override
    protected void onPauseLazy() {
        super.onPauseLazy();
        bannerComponent.stopAutoPlay();
    }

    public void refresh() {
        if(textView != null) {
            textView.setText(textView.getText() + "\n" + "切换到这啦！！！");
        }
    }

    @Override
    public void onDestroyViewLazy() {
        super.onDestroyViewLazy();
        handler.removeCallbacksAndMessages(null);
    }

    private Handler handler;

    private int[] images = {R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4};
    private IndicatorViewPager.IndicatorViewPagerAdapter adapter = new IndicatorViewPager.IndicatorViewPagerAdapter() {

        @Override
        public View getViewForTab(int position, View convertView, ViewGroup container) {
            if (convertView == null) {
                convertView = inflate.inflate(R.layout.tab_guide, container, false);
            }
            return convertView;
        }

        @Override
        public View getViewForPage(int position, View convertView, ViewGroup container) {
            if (convertView == null) {
                convertView = new ImageView(getApplicationContext());
                convertView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            }
            ImageView imageView = (ImageView) convertView;
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setImageResource(images[position]);
            return convertView;
        }

        @Override
        public int getItemPosition(Object object) { //这样，刷新adapter时才会立即生效
            return RecyclingPagerAdapter.POSITION_NONE;
        }

        @Override
        public int getCount() {
            return images.length;
        }
    };
}
